/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.IMLBroadcaster;
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.tools.MLEventsReflector;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class DuplicatedMCSInClasses extends MLBroadcastClass implements IMLBroadcasterListener {
		
		//DATA:	MLEventInfo //"real" event info
		public const event_EventReflection : String = 'EventReflection';
		
		private var duplicator:DuplicatedMCSNumberController;
		private var mcsInClasses:Array;
		private var mcsInClassesEvents:MLEventsReflector;
		private var owner:IClassForMCFactory;

		public function DuplicatedMCSInClasses(owner:IClassForMCFactory, duplicatedMC:MovieClip) {
			this.owner=owner;
			duplicator=new DuplicatedMCSNumberController(new MCDuplicator(duplicatedMC));
			duplicator.addListener(this);
			mcsInClasses=[];
			mcsInClassesEvents=new MLEventsReflector(true);
			addEventsMapping(mcsInClassesEvents);
		}
		
		public function getDuplicator():DuplicatedMCSNumberController {
			return duplicator;
		}

		public function getCurrentMCSInClasses():Array {
			return mcsInClasses.slice(0, duplicator.getCurrentVisibleMCSSum());
		}
		
		public function addOneAndGet():Object {
			duplicator.addOne();
			return mcsInClasses[duplicator.getCurrentVisibleMCSSum()-1];
		}
		
		public function getClassWithMCByIndex(index:Number):Object {
			return mcsInClasses[index];
		}
		
//********************************************************************************************
//		events for DuplicatorMCSInClasses
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case duplicator:
					switch(ev.event) {
						case duplicator.event_NewMC:
							var classWithMC:Object=owner.createClassForMC(this, ev.data.mc, mcsInClasses.length);
							mcsInClasses.push(classWithMC);
							if(classWithMC is IMLBroadcaster){
								mcsInClassesEvents.addObject(IMLBroadcaster(classWithMC));
							}
						break;
					}
				break;
			}
		}

	}
}
